package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpLogService;
import com.itheima.service.EmpService;
import com.itheima.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Autowired
    private EmpLogService empLogService;
    @Autowired
    private DeptService deptService;

    @Override
    public PageResult<Emp> page(EmpQueryParam empQueryParam) {
        //1. 设置分页参数(PageHelper)
        //PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());
        //2. 执行查询
        //List<Emp> empList = empMapper.list(empQueryParam);
        //3. 解析查询结果, 并封装
        //Page<Emp> p = (Page<Emp>) empList;
        //return new PageResult<Emp>(p.getTotal(), p.getResult());
        //创建分页对象,并指定页码和每页记录数
        IPage<Emp> page = new Page<>(empQueryParam.getPage(), empQueryParam.getPageSize());
        //编写查询条件
        LambdaQueryWrapper<Emp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasLength(empQueryParam.getName()), Emp::getName, empQueryParam.getName());
        queryWrapper.eq(empQueryParam.getGender() != null, Emp::getGender, empQueryParam.getGender());
        queryWrapper.between(empQueryParam.getBegin() != null && empQueryParam.getEnd() != null,
                Emp::getEntryDate, empQueryParam.getBegin(), empQueryParam.getEnd());
        //调用IService中的方法,传入分页对象,和分页查询条件
        page = super.page(page, queryWrapper);
        //获取查询结果集 (集合中  Emp对象缺少部门名称数据)
        List<Emp> emps = page.getRecords();
        emps.forEach(emp -> {
            //根据员工ID查询员工所属部门名称
            Dept dept = deptService.getById(emp.getDeptId());
            if (dept != null) {
                //获取部门名称,并设置到当前的emp对象中
                emp.setDeptName(dept.getName());
            }
        });
        //获取查询结果,并封装成PageResult对象
        return new PageResult<>(page.getTotal(), emps);
    }

    @Transactional(rollbackFor = {Exception.class}) //事务管理 - 默认出现运行时异常RuntimeException才会回滚
    @Override
    public void insert(Emp emp) throws Exception {
        try {
            //1. 保存员工基本信息
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            // empMapper.insert(emp);
            save(emp);

            //2. 保存员工工作经历信息
            List<EmpExpr> exprList = emp.getExprList();
            if (!CollectionUtils.isEmpty(exprList)) {
                //遍历集合, 为empId赋值
                exprList.forEach(empExpr -> {
                    empExpr.setEmpId(emp.getId());
                });
                empExprMapper.insertBatch(exprList);
            }
        } finally {
            //记录操作日志
            EmpLog empLog = new EmpLog(null, LocalDateTime.now(), "新增员工:" + emp);
            empLogService.insertLog(empLog);
        }
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void delete(List<Integer> ids) {
        //1. 批量删除员工基本信息
        empMapper.deleteByIds(ids);

        //2. 批量删除员工的工作经历信息
        empExprMapper.deleteByEmpIds(ids);
    }

    @Override
    public Emp getInfo(Integer id) {
        return empMapper.getById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Emp emp) {
        //1. 根据ID修改员工的基本信息
        emp.setUpdateTime(LocalDateTime.now());
        //empMapper.updateById(emp);
        updateById(emp);

        //2. 根据ID修改员工的工作经历信息
        //2.1 先根据员工ID删除原有的工作经历
        empExprMapper.deleteByEmpIds(Arrays.asList(emp.getId()));

        //2.2 再添加这个员工新的工作经历
        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)) {
            exprList.forEach(empExpr -> empExpr.setEmpId(emp.getId()));
            empExprMapper.insertBatch(exprList);
        }
    }

    @Override
    public List<Emp> list() {
        return empMapper.findAll();
    }

    @Override
    public LoginInfo login(Emp emp) {
        //1. 调用mapper接口, 根据用户名和密码查询员工信息
        //Emp e = empMapper.selectByUsernameAndPassword(emp);
        Emp e = getOne(Wrappers.<Emp>lambdaQuery()
                .eq(Emp::getUsername, emp.getUsername())
                .eq(Emp::getPassword, emp.getPassword()));
        //2. 判断: 判断是否存在这个员工, 如果存在, 组装登录成功信息
        if (e != null) {
            log.info("登录成功, 员工信息: {}", e);
            //生成JWT令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", e.getId());
            claims.put("username", e.getUsername());
            String jwt = JwtUtils.generateToken(claims);

            return new LoginInfo(e.getId(), e.getUsername(), e.getName(), jwt);
        }

        //3. 不存在, 返回null
        return null;
    }
}
